# 计算怪物属性和伤害
class Game_EnemyInfo
  # 定义怪物各项属性使其可以被外部访问
  attr_reader :ehp
  attr_reader :eatk
  attr_reader :edef
  attr_reader :eskill

  # 初始化
  def initialize(id)
  	@id = id
  	@ehp = $data_enemies[id].maxhp
  	@eatk = $data_enemies[id].atk
  	@edef = $data_enemies[id].pdef
    tmp = $data_enemies[id].name.split(":")[1]
    @eskill = (tmp == nil ? 0 : tmp.to_i)
  end

  # 计算伤害，无法战胜返回-1
  def getDamage
  	# 获取勇士属性
  	a_hp = $game_party.actors[0].hp
  	a_atk = $game_party.actors[0].str
  	a_def = $game_party.actors[0].dex

  	# 勇士防御低于怪物攻击则无法战胜
  	return -1 if a_atk <= @edef

    # 计算勇士每次对怪物造成的伤害
    dam = a_atk - @edef
    # 怪物坚固的情况
    dam = 1 if @eskill == 5

  	# 计算被攻击次数
  	t = @ehp / dam
  	t -= 1 if @ehp % dam == 0
    # 怪物先攻的情况
    t += 1 if @eskill == 1
    # 怪物连击的情况
    t *= 2 if @eskill == 3

    # 计算怪物每次对勇士造成的伤害
    dam = [@eatk - a_def, 0].max
    # 怪物魔攻的情况
    dam = @eatk if @eskill == 2

  	# 计算伤害
  	d = t * dam
    # 怪物吸血的情况
    d += a_hp / 3 if @eskill == 4

  	# 返回伤害值
  	return d
  end
end

# 处理敌人信息的模块
module EnemyInfo
  # 获取敌人信息(排序后)
  def self.getSort
    data = []
    for ev in $game_map.events.values
      # 如果不是怪物或者怪物已经死亡则继续
      next unless $game_map.events[ev.id].event.name[0,2] == '_M'
      key = [$game_map.map_id, ev.id, 'A']
      next if $game_self_switches[key] == true
      # 将怪物加入data
      m_id = $game_map.events[ev.id].event.name[2,5].to_i
      data.push(m_id) unless data.include?(m_id)
    end
    return data.sort
  end

  # 获取敌人信息(获取坐标)
  def self.getCoordinate
    data = []
    for ev in $game_map.events.values
      # 如果不是怪物或者怪物已经死亡则继续
      next unless $game_map.events[ev.id].event.name[0,2] == '_M'
      key = [$game_map.map_id, ev.id, 'A']
      next if $game_self_switches[key] == true
      # 将怪物加入data
      m_id = $game_map.events[ev.id].event.name[2,5].to_i
      data.push([m_id, ev.x, ev.y, ev.id])
    end
    return data
  end
end
